<?php
	include "../public/commom/config.php";//引入配置连接数据库文件
	include "../public/commom/commom.php";//引入配置图片文件
	include "../public/commom/timename.php";//引入配置时间文件
	
	if($_POST) {
		$user_id = $_SESSION["user_id"];
		$tuser_id = isset($_POST["tuser_id"]) ? $_POST["tuser_id"] : 0;
		$currentnum = isset($_SESSION["currentnum"]) ? $_SESSION["currentnum"] : 0;
		
		$sql = "SELECT privateletter.*,user.headportrait FROM privateletter LEFT JOIN user ON (privateletter.tuser_id = user.id) WHERE (privateletter.tuser_id = '$tuser_id' AND privateletter.user_id = '$user_id') ORDER BY time DESC";//sql语句 - 查找该用户所有私信
		
		$result = mysqli_query($conn, $sql);//result - 执行sql语句
		
		$chatnum = mysqli_num_rows($result);//用户聊天数量
		
		if($currentnum < $chatnum) {
			$poor = $chatnum - $currentnum;
			
			$_SESSION["currentnum"] = $chatnum;//用户聊天数量
			
			if($poor > 0) {
				$chatlist = array();
				
				$i = 0;
				
				while($row = mysqli_fetch_array($result)) {
					$chatlist[$i++] = $row;	
				}
				
				$chat = array("chatlist" => $chatlist, "poor" => "$poor");//数组
				
				echo json_encode($chat);//以json格式输出数据
				
				mysqli_free_result($result);//释放内存
				
				mysqli_close($conn);//关闭数据库连接
			}
		}
	}
?>